<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>knnclass.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>y&nbsp;</span>=&nbsp;<span class=defun_name>knnclass</span>(<span class=defun_in>X,model</span>)<br>
<span class=h1>%&nbsp;KNNCLASS&nbsp;k-Nearest&nbsp;Neighbours&nbsp;classifier.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;knnclass(X,model)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;input&nbsp;feature&nbsp;vectors&nbsp;X&nbsp;are&nbsp;classified&nbsp;using&nbsp;the&nbsp;K-NN</span><br>
<span class=help>%&nbsp;&nbsp;rule&nbsp;defined&nbsp;by&nbsp;the&nbsp;input&nbsp;model.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;to&nbsp;be&nbsp;classified.</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Model&nbsp;of&nbsp;K-NN&nbsp;classfier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_prototypes]&nbsp;Prototypes.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_prototypes]&nbsp;Labels&nbsp;of&nbsp;prototypes.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.K&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;used&nbsp;nearest-neighbours.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Classified&nbsp;labels&nbsp;of&nbsp;testing&nbsp;data.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;trn&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;tst&nbsp;=&nbsp;load('riply_tst');</span><br>
<span class=help>%&nbsp;&nbsp;ypred&nbsp;=&nbsp;knnclass(tst.X,knnrule(trn,5));</span><br>
<span class=help>%&nbsp;&nbsp;cerror(&nbsp;ypred,&nbsp;tst.y&nbsp;)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;KNNRULE.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>(c)</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox,&nbsp;(C)&nbsp;1999-2003,</span><br>
<span class=help1>%&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac,</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;,</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;engineering&lt;/a&gt;,</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;19-may-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;18-sep-2002,&nbsp;V.Franc</span><br>
<br>
<hr>
X=c2s(X);<br>
model=c2s(model);<br>
<br>
<span class=keyword>if</span>&nbsp;~isfield(model,<span class=quotes>'K'</span>),&nbsp;model.K=1;&nbsp;<span class=keyword>end</span>;<br>
<br>
y&nbsp;=&nbsp;knnclass_mex(X,model.X,model.y,&nbsp;model.K);<br>
<br>
<span class=jump>return</span>;&nbsp;<br>
<span class=comment>%&nbsp;EOF</span><br>
<br>
</code>
